I'm consistently getting the error
Not bound to a valid Camera
when attempting to implement the imageCapture use case. I'm implementing the feature in Java following the camerax tutorial here.
The preview use case binds correctly and I can access the camera when I launch the activity, but when I tap on the "take photo" button, I get the error below:
Photo capture failed: ${exception.message}
androidx.camera.core.ImageCaptureException: Not bound to a valid Camera [ImageCapture:androidx.camera.core.ImageCapture-9aad1b57-8fb6-4a6e-b528-9347343369b6]
at androidx.camera.core.ImageCapture.lambda$sendImageCaptureRequest$6$ImageCapture(ImageCapture.java:825)
at androidx.camera.core.-$$Lambda$ImageCapture$2B91NcXQyr59NDscigcxsZb94mc.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Anyone facing a similar issue? Any pointers?
cameraProvider.bindToLifecycle(
this, cameraSelector, preview, imageCapture)
To fix this problem you have to bind the collections of the UseCase to lifeCycleOwner.
What is the useCase:
A UseCase provides functionality to map the set of arguments in a use case to arguments that are usable by a camera. UseCase also will communicate of the active / inactive state to the Camera.
First you must have the dependencies below in your build.gradel :
dependencies {
/*
rest of your implementation
*/
// CameraX core library using the camera2 implementation
def camerax_version = "1.1.0-alpha03"
// The following line is optional, as the core library is included indirectly by camera-camera2
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
// If you want to additionally use the CameraX Lifecycle library
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
}
And now you can bind it by using :
public androidx.camera.core.Camera bindToLifecycle(@NonNull @NotNull androidx.lifecycle.LifecycleOwner lifecycleOwner,
@NonNull @NotNull androidx.camera.core.CameraSelector cameraSelector,
@NonNull @NotNull androidx.camera.core.UseCase... useCases)
For exmaple i have this code in my MainActivity :
For java :
UseCase imageCapture = new ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
.build();
ListenableFuture<ProcessCameraProvider> providerListenableFuture = ProcessCameraProvider.getInstance(this);
ProcessCameraProvider processCameraProvider = providerListenableFuture.get();
processCameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA, imageCapture);
And for Kotlin :
val outputFileOptions = ImageCapture.OutputFileOptions.Builder(file).build()
val imageCapture = ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
.build()
val cameraProviderFuture: ListenableFuture<ProcessCameraProvider> = ProcessCameraProvider.getInstance(this)
val processCameraProvider = cameraProviderFuture.get()
processCameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA, imageCapture)
For more details consult the implementation of the CameraX on the official documentation
Ensure you also bind for preview and imageCapture when doing startCamera
// Bind use cases to camera
cameraProvider.bindToLifecycle(this, cameraSelector, imageCapture, preview)